import { defineConfig } from 'vite';
import path from 'path';
import vue from '@vitejs/plugin-vue';
// import styleImport from 'vite-plugin-style-import';
import viteCompression from 'vite-plugin-compression';
import { injectHtml } from 'vite-plugin-html';

// https://vitejs.dev/config/
// mode:  'development' for serve, 'production' for build

export default ({ mode }) =>
  defineConfig({
    plugins: [
      vue(),
      //gzip压缩
      viteCompression(),
      injectHtml({ injectData: { mode } }),
      //按需引入element
      // styleImport({
      //   libs: [
      //     {
      //       libraryName: 'element-plus',
      //       resolveStyle: name => {
      //         return `element-plus/theme-chalk/${name}.css`;
      //       },
      //       resolveComponent: name => {
      //         return `element-plus/${name}`;
      //       },
      //     },
      //   ],
      // }),
    ],
    // html 静态页面目录
    root: './',
    // index.html 访问资源增加的url
    base: './',
    // mode: command,
    publicDir: 'static',

    // 路径别名
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'),
        '@c': path.resolve(__dirname, './src/components'),
        '@a': path.resolve(__dirname, './src/assets'),
        '@v': path.resolve(__dirname, './src/views'),
        '@p': path.resolve(__dirname, './src/plugins'),
        '@utils': path.resolve(__dirname, './src/utils'),
        '@static': path.resolve(__dirname, './static'),
      },
    },
    css: {
      modules: {
        scopeBehaviour: 'local',
      },
      // postcss.config.js same
      // postcss: {},
    },
    //
    json: {
      // Whether to support named imports from .json files.
      // namedExports: true
      // json 文件是否以 字符串的形式引入，这种形式。这比Object形式性能要高得多，尤其是在JSON文件较大时。
      // stringify:
    },
    // esbuild 打包的参数，主要应用于 ts、jsx、tsx，需要参考esbuild
    // esbuild:{},
    /**
     * 可以让资源不被 plugin （没有使用 enforce: 'pre' 的） 处理
     * https://github.com/vitejs/vite/blob/main/packages/vite/src/node/constants.ts
     */
    // assetsInclude:
    logLevel: 'info',
    // 可以避免 Vite 清屏而错过在终端中打印某些关键信息
    clearScreen: false,
    // -------------------- Server Options start
    server: {
      host: '0.0.0.0',
      port: 3001,
      //设为 true 时若端口已被占用则会直接退出，而不是尝试下一个可用端口
      strictPort: false,
      // Enable TLS + HTTP/2. Note this downgrades to TLS only when the server.proxy option is also used.
      // The value can also be an options object passed to https.createServer().
      // https: string | obj
      //打开浏览器 ,可以设置为固定的html路径
      open: false,
      //代理
      proxy: {
        "/mics-datahub": {
          target: "http://172.23.0.6:8088",
          target: "http://10.51.13.140:8808",
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ""),
        },
        "/v3d": {
          target: "http://10.51.13.140:8808",
          target: "http://172.23.0.6:8088",
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ""),
        },
        "/ws": {
          target: "http://10.51.13.140:8808",
          // target: "http://172.23.0.6:8088",
          ws: true,
          changeOrigin: true,
        
        },
      },
      // cors: // 跨域配置项，默认允许任何源
      force: true, // 设置为 true 强制使依赖预构建。
      // hmr:
      // https://github.com/paulmillr/chokidar#api
      // watch:
    },
    // --------------------- build options start ---------------
    build: {
      /**
       * * Default: 'modules' - Similar to `@babel/preset-env`'s targets.esmodules,
       * transpile targeting browsers that natively support es module imports. Also
       * injects a light-weight dynamic import polyfill.
       * https://caniuse.com/es6-module
       *
       * esnext 会以最小化程度进行编译，谨慎使用
       * * Another special value is 'esnext' - which only performs minimal trasnpiling
       * (for minification compat) and assumes native dynamic imports support.
       */
      // target: ""
      // 打包后的文件路径
      outDir: './dist',
      // By default, Vite will empty the outDir on build if it is inside project root. 删除之前的打包文件
      emptyOutDir: true,
      rollupOptions: {
        input: {
          main: path.join(__dirname, 'index.html'),
          // nested: resolve(__dirname, 'nested/index.html')
        },
      },
      // 静态资源路径
      assetsDir: 'assets',
      /**
       * Imported or referenced assets that are smaller than this threshold will be inlined as base64 URLs to avoid extra http requests. Set to 0 to disable inlining altogether.
       * 静态资源小于多少时使用base64 形式内联写入
       * */
      assetsInlineLimit: 8 * 1024,
      /**
       * false：所有的 css 会被打包到一个css文件中
       * true：css样式文件会拆分到各个异步chunk 中
       */
      cssCodeSplit: true,
      sourcemap: false,
      // https://rollupjs.org/guide/en/#big-list-of-options
      // rollupOptions:
      // Options to pass on to @rollup/plugin-commonjs.   https://github.com/rollup/plugins/tree/master/packages/commonjs
      // commonjsOptions:
      //
      // lib:
      // When set to true, the build will also generate a manifest.json
      manifest: true,
      minify: false,
      // Additional minify options to pass on to Terser.
      // terserOptions:
      // Constructor options to pass on to clean-css.  https://github.com/jakubpawlowicz/clean-css#constructor-options
      // cleanCssOptions:
      //
      // write:
      // 是否启用 brotli 压缩，IE 不支持
      brotliSize: false,
      // chunk 超大警告显示
      chunkSizeWarningLimit: 300,
    },
    // -----------------  Dep Optimization Options   Related: Dependency Pre-Bundling
    optimizeDeps: {
      // 以指定依赖作为入口
      // entries:
      // 不参与 pre-build 的文件
      // exclude:
      // include:
    },
    // ---------- SSR Options in minor releases
  });
